//
//  ViewController.m
//  AMQQShare-iOS
//
//  Created by edz on 2019/8/14.
//  Copyright © 2019 edz. All rights reserved.
//

#import "ViewController.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/QQApiInterfaceObject.h>
static NSString *kImageTagName = @"QQ_TAG_JUMP_APP";
static NSString *kMessageExt = @"这是第三方带的测试字段";
@interface ViewController ()
- (IBAction)shareVideoTofriend:(id)sender;
- (IBAction)shareImageToFriend:(id)sender;
- (IBAction)shareTextToFriend:(id)sender;
- (IBAction)shareUrlToFriend:(id)sender;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
//分享链接给好友
- (IBAction)shareUrlToFriend:(id)sender {
//    QQshareFFI *share = [[QQshareFFI alloc]init];
//    [share QQShareLinkAction: andTitle:@"百度网"];
    NSString *utf8String = @"www.baidu.com";
    NSString *title = @"百度网";
    NSString *description = @"";
    NSString *previewImageUrl = @"";
    QQApiNewsObject *newsObj = [QQApiNewsObject
                                objectWithURL:[NSURL URLWithString:utf8String]
                                title:title
                                description:description
                                previewImageURL:[NSURL URLWithString:previewImageUrl]];
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
    //将内容分享到qq
    dispatch_async(dispatch_get_main_queue(), ^{
        //回调或者说是通知主线程刷新，
        QQApiSendResultCode sent = [QQApiInterface sendReq:req];
        [self handleSendResult:sent];
        
        //QQApiSendResultCode sent = [QQApiInterface SendReqToQZone:req];
    });
}

//分享视频到好友

- (IBAction)shareVideoTofriend:(id)sender {
   
    
//    QQshareFFI *share = [[QQshareFFI alloc]init];
//    [share QQShareVideoAction:@"https://www.bilibili.com/video/av46288097/"];
    NSString *video = @"https://www.bilibili.com/video/av46288097/";
    if (video.length<=0)
    {
        return;
    }
    NSString *previewPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"mway.jpg"];
    NSData* previewData = [NSData dataWithContentsOfFile:previewPath];
    NSString *utf8String = video;
    QQApiVideoObject *videoObj = [QQApiVideoObject objectWithURL:[NSURL URLWithString:utf8String ? : @""]
                                                           title:kImageTagName
                                                     description:kMessageExt
                                                previewImageData:previewData];
    [videoObj setFlashURL:[NSURL URLWithString:video]];
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:videoObj];
    
    //回调或者说是通知主线程刷新，
    dispatch_async(dispatch_get_main_queue(), ^{
        
        QQApiSendResultCode sent = [QQApiInterface sendReq:req];
        [self handleSendResult:sent];
        
        //QQApiSendResultCode sent = [QQApiInterface SendReqToQZone:req];
    });
}
//分享图片给好友

- (IBAction)shareImageToFriend:(id)sender {

   //网络图片
    NSString *imageUrl = @"https://zhangzifan.com/uploads/2016/12/https-protocol.png";
     //本地图片
//    imageUrl = @"shareimage.jpeg"]
//
    //图片数据不能为空并且大小不能超过10M
    if (imageUrl.length<=0)
    {
        return;
    }
    NSData *data;
    if ([imageUrl hasPrefix:@"http"])
    {
        //        imageUrl = [NSString stringWithString:[imageUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        
        data = [NSData  dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
    }
    else
    {
        data = [NSData  dataWithContentsOfFile:imageUrl];
    }
    
    //    UIImage *image =  [UIImage imageWithData:data];
    //    NSData *imageData = [[NSFileManager defaultManager] contentsAtPath:image];
    //    NSString *imgPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"mway.jpg"];
    //    NSData *imgData = [NSData dataWithContentsOfFile:imgPath];
    QQApiImageObject *imgObj = [QQApiImageObject objectWithData:data
                                               previewImageData:data
                                                          title:kImageTagName
                                                    description:kMessageExt];
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:imgObj];
    //将内容分享到qq
    dispatch_async(dispatch_get_main_queue(), ^{
        //回调或者说是通知主线程刷新，
        QQApiSendResultCode sent = [QQApiInterface sendReq:req];
        [self handleSendResult:sent];
        
        //QQApiSendResultCode sent = [QQApiInterface SendReqToQZone:req];
    });
}

//分享文本

- (IBAction)shareTextToFriend:(id)sender {
    QQApiTextObject *txtObj = [QQApiTextObject objectWithText:@"Hello World!!"];
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:txtObj];
    //将内容分享到qq
    dispatch_async(dispatch_get_main_queue(), ^{
        //回调或者说是通知主线程刷新，
        QQApiSendResultCode sent = [QQApiInterface sendReq:req];
        [self handleSendResult:sent];
        
    });
}
- (void)handleSendResult:(QQApiSendResultCode)sendResult
{
    switch (sendResult)
    {
            
        case EQQAPIAPPNOTREGISTED:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"App未注册" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            
            break;
        }
        case EQQAPIMESSAGECONTENTINVALID:
        case EQQAPIMESSAGECONTENTNULL:
        case EQQAPIMESSAGETYPEINVALID:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"发送参数错误" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            
            break;
        }
        case EQQAPIQQNOTINSTALLED:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您的设备未安装手机QQ" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            
            break;
        }
        case EQQAPIQQNOTSUPPORTAPI:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您的设备未安装手机QQ" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            
            break;
        }
        case EQQAPISENDFAILD:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"发送失败" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            
            break;
        }
        case EQQAPIVERSIONNEEDUPDATE:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"当前QQ版本太低，需要更新" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            break;
        }
        default:
        {
            break;
        }
    }
}

#pragma mark QQ分享回调代理
/* 返回码 对照说明
 0   成功
 -1  参数错误
 -2  该群不在自己的群列表里面
 -3  上传图片失败
 -4  用户放弃当前操作
 -5  客户端内部处理错误
 */
-(void)shareSuccssWithQQCode:(NSInteger)code{
    NSLog(@"code %ld",(long)code);
    if (code == 0) {
        UIAlertView *aler = [[UIAlertView alloc]initWithTitle:@"提示" message:@"分享成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [aler show];
    }else{
        UIAlertView *aler = [[UIAlertView alloc]initWithTitle:@"提示" message:@"分享失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [aler show];
    }
}
@end
